package mr.ali.webvideo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        // 设置连接超时时间
        factory.setConnectTimeout(5000); // 5秒连接超时
        // 设置读取超时时间
        factory.setReadTimeout(10000); // 10秒读取超时
        
        RestTemplate restTemplate = new RestTemplate(factory);
        // 设置消息转换器，确保正确处理中文和JSON
        restTemplate.setMessageConverters(Arrays.asList(
            new StringHttpMessageConverter(StandardCharsets.UTF_8),
            new MappingJackson2HttpMessageConverter()
        ));
        return restTemplate;
    }
}